package vn.orderonline.core.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import vn.orderonline.core.commons.utils.StringUtils;
import vn.orderonline.core.dao.common.GenericDaoImpl;
import vn.orderonline.core.dao.entity.Admin;

@Repository
public class AdminDaoImpl extends GenericDaoImpl<Admin, Integer> implements
		AdminDao {

	@SuppressWarnings("unchecked")
	public List<Admin> search(String adminName, String adminEmail,
			String adminLoginId, String adminPassword) {

		Criteria criteria = getSession().createCriteria(Admin.class);

		if (!StringUtils.isNullOrEmpty(adminName)) {
			criteria.add(Restrictions.eq("adminName", adminName));
		}
		if (!StringUtils.isNullOrEmpty(adminEmail)) {
			criteria.add(Restrictions.eq("adminEmail", adminEmail));
		}
		if (!StringUtils.isNullOrEmpty(adminLoginId)) {
			criteria.add(Restrictions.eq("adminLoginId", adminLoginId));
		}
		if (!StringUtils.isNullOrEmpty(adminPassword)) {
			criteria.add(Restrictions.eq("adminPassword", adminPassword));
		}

		return criteria.list();
	}

	@Override
	public Object[] getAdminByAdminLoginId(String username) {
		String hql = "Select admin, role "
				+ "From Admin admin, Role role "
				+ "Where admin.roleId = role.roleId And admin.adminLoginId = :username";
		Query query = getSession().createQuery(hql);
		query.setParameter("username", username);
		return (Object[]) query.uniqueResult();
	}

	@Override
	public Boolean checkExistedAccount(String username, String password) {
		Criteria criteria = getSession().createCriteria(Admin.class);
		criteria.add(Restrictions.eq("adminLoginId", username));
		criteria.add(Restrictions.eq("adminPassword", password));
		return (criteria.uniqueResult() != null) ? true : false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getAllAdmin() {
		String hql = "Select admin, role " + "From Admin admin, Role role "
				+ "Where admin.roleId = role.roleId";
		Query query = getSession().createQuery(hql);
		return query.list();
	}

	@Override
	public String getAdminNameByAccountName(String adminLoginId) {
		Criteria criteria = getSession().createCriteria(Admin.class);
		criteria.setProjection(Projections.property("adminName"));
		criteria.add(Restrictions.eq("adminLoginId", adminLoginId));
		return (String) criteria.uniqueResult();
	}

	@Override
	public Boolean checkExistedEmail(String email) {
		Criteria criteria = getSession().createCriteria(Admin.class);
		criteria.add(Restrictions.eq("adminEmail", email));
		return criteria.uniqueResult() != null ? true : false;
	}

	@Override
	public Boolean checkExistedAdminLoginId(String adminLoginId) {
		Criteria criteria = getSession().createCriteria(Admin.class);
		criteria.add(Restrictions.eq("adminLoginId", adminLoginId));
		return criteria.uniqueResult() != null ? true : false;
	}

}
